1515D - Phoenix and Socks - CodeForces Solution


greedy sortings two pointers *1500

Please click on ads to support us..

Python Code:

def f(A,L,n):
    dict = {}
    for i in range(n):
        if(A[i] in dict.keys()):
            if(i+1 <= L):
                dict[A[i]][0] += 1
            else:
                dict[A[i]][1] += 1
        else:
            if(i+1 <= L):
                dict[A[i]] = [1,0]
            else:
                dict[A[i]] = [0,1]
    ans = 0
    l = r = 0
    for i in dict.keys():
        if(dict[i][0] > dict[i][1]):
            dict[i][0] -= dict[i][1]
            dict[i][1] = 0
            l += dict[i][0]
        else:
            dict[i][1] -= dict[i][0]
            dict[i][0] = 0
            r += dict[i][1]
        if(l==r):
        return l
    elif(l<r):
        r -= l
        ans += l
        odd = 0
        for i in dict.keys():
            if(l>0 and dict[i][1]%2==1):
                dict[i][1] -= 1
                l -= 1
            elif(dict[i][1]%2==1):
                odd += 1
        if(odd==0):
            ans += (r//2)
            return ans
        else:
            ans += ((r-odd)//2)+(odd)
            return ans
    else:
        l -= r
        ans += r
        odd = 0
        for i in dict.keys():
            if(r>0 and dict[i][0]%2==1):
                dict[i][0] -= 1
                r -= 1
            elif(dict[i][0]%2==1):
                odd += 1
                if(odd==0):
            ans += (l//2)
            return ans
        else:
            ans += ((l-odd)//2)+(odd)
            return ans
tc = int(input())
while(tc>0):  
    X = [int(i) for i in input().split()]
    n = X[0]
    l = X[1]
    A = [int(i) for i in input().split()]
    print(f(A,l,n))
    tc -= 1

    

            
            
                    
            

C++ Code:

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

#define all(v) v.begin(), v.end()
#define int long long
#define ll long long
#define pb push_back
#define endl "\n"
#define bn begin()
#define en end()
#define ppb pop_back
#define ff first
#define ss second
#define pii pair<int, int>
#define loop(i, a, b) for (int i = a; i < b; i++)
#define bs binary_search
const ll inf = 1e18, mod = 1e9 + 7;
#define add_mod(a, b) (a % mod + b % mod) % mod
#define mul_mod(a, b) (a % mod * b % mod) % mod
#define sub_mod(a, b) (mod + a % mod - b % mod) % mod
#define compar \
    bool compare(int a, int b) { return a > b; }
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> pbds; // find_by_order, order_of_key
#define fast                          \
    ios_base::sync_with_stdio(false); \
    cin.tie(NULL);                    \
    cout.tie(NULL);
#ifndef ONLINE_JUDGE
    #define file  freopen("Error.txt", "w", stderr);freopen("input.txt", "r", stdin ); freopen("output.txt", "w", stdout);
#else
    #define file
#endif
#define per(x) fixed << setprecision(x) << // syntax:-cout<<per(jitni decimal place chahiyaen)variable name;
// bool compare(pair<int,int> a,pair<int,int> b){if(a.first==b.first)return a.second<b.second;return a.first<b.first;}
// if an element have a divisor i less than sqrt(n) the it will definately have n/i as a divisor
//  (a/b) %m = a%m *binpow(b,m-2) provided b and m are co-prime.
//  negative number ka mod tackle karne ke liyaen uss number me mod jod kar mod kar lo
int binpow(int a, int b)
{
    int res = 1LL;
    while (b > 0)
    {
        if (b & 1)
            res = (res%mod * a%mod)%mod;
        a = (a%mod * a%mod)%mod;
        b >>= 1;
    }
    return res;
}
int gcd(int a, int b) {
    if (!a || !b)
        return a | b;
    unsigned shift = __builtin_ctz(a | b);
    a >>= __builtin_ctz(a);
    do {
        b >>= __builtin_ctz(b);
        if (a > b)
            swap(a, b);
        b -= a;
    } while (b);
    return a << shift;
}
int lcm(int a,int b)
{
    return ((a*b)/gcd(a,b));
}
int ceil_div(int a,int b)
{
    int val = (a / b) + ((a % b) != 0ll);
    return val;
}
int modinv(int a, int m)
{
    int m0 = m;
    int y = 0LL, x = 1LL;
    if (m == 1LL)
        return 0LL;
    while (a > 1LL)
    {
        int q = a / m;
        int t = m;
        m = a % m, a = t;
        t = y;
        y = x - q * y;
        x = t;
    }
    if (x < 0LL)
        x += m0;

    return x;
}

void printDivisors(int n, vector<int> &v)
{
    // vector<int> v;
    // Note that this loop runs till square root
    for (int i = 1; i*i <= n; i++)
    {
        if (n % i == 0)
        {
            // If divisors are equal, print only one
            if (n / i == i)
                v.pb(i);

            else
            { // Otherwise print both
                // if (i != 1)
                    v.pb(i);
                v.pb(n / i);
            }
        }
    }
}
// vector<int> x={0,0,1,-1};
// vector<int> y={-1,1,0,0};
// vector<char> path={'L','R','D','U'};
// vector<int> x={0,0,1,-1,1,1,-1,-1};
// vector<int> y={1,-1,0,0,1,-1,1,-1};

// everyday 1 codeforces and 1 leetcode in the morning  
// everyday 1 codeforces and 1 leetcode in the evening
// always take the input correctly.

// if p divides x+k and y+k the p also devides (y-x) for (y>x)
// (a|x)-(b&x) == (a|x) ^ (b&x)
// if a%b==0 then a must contain contain all the prime factors of b in same or greater freq. along with other prime numbers
// for interactive problem make sure ki (#define endl as "\n") ko comment out karke endl likho
// when even we are ask find in each sub array or subseq try to find how an individual element is contributing in the final answer of

void fun()
{
    int n,l,r;
    cin>>n>>l>>r;
    map<int,int> m;
    loop(i,0,l)
    {
        int a;
        cin>>a;
        m[a]++;
    }
    loop(j,0,r)
    {
        int a;
        cin>>a;
        m[a]--;
    }
    int ol=0;
    int el=0;
    int odr=0;
    int er=0;
    int ct=0;
    for(auto it: m)
    {
        if(it.ss<0)
        {
            if(it.ss%2)
            {
                odr++;
                er+=max(abs(it.ss)-1ll,0ll);
            }
            else
            {
                er+=abs(it.ss);
            }
        }
        else
        {
            if(it.ss%2)
            {
                ol++;
                el+=max(it.ss-1ll,0ll);
            }
            else
            {
                el+=it.ss;

            }   
        }
    }
    int ans=min(ol,odr);
    ol-=ans;
    odr-=ans;
    if(ol!=0)
    {
        int a=min(er,ol);
        ans+=a;
        er-=a;
        ol-=a;
    }

    if(odr!=0)
    {
        int a=min(el,odr);
        ans+=a;
        el-=a;
        odr-=a;
    }
    ans+=(ol+odr+er/2+el/2);
    cout<<ans<<endl;
    // cout<<ol<<" "<<el<<" "<<odr<<" "<<er<<endl;
}

signed main()
{
    fast 
    file
    int t;
    cin >> t;
    // t=1;
    while (t--)
    {
        fun();
    }
    return 0;
}        


Comments

Submit
0 Comments
More Questions

1562A - The Miracle and the Sleeper
1216A - Prefixes
1490C - Sum of Cubes
868A - Bark to Unlock
873B - Balanced Substring
1401D - Maximum Distributed Tree
1716C - Robot in a Hallway
1688B - Patchouli's Magical Talisman
99A - Help Far Away Kingdom
622B - The Time
1688C - Manipulating History
1169D - Good Triple
1675B - Make It Increasing
588A - Duff and Meat
1541B - Pleasant Pairs
1626B - Minor Reduction
1680A - Minimums and Maximums
1713A - Traveling Salesman Problem
1713B - Optimal Reduction
1710A - Color the Picture
1686B - Odd Subarrays
251A - Points on Line
427C - Checkposts
1159A - A pile of stones
508A - Pasha and Pixels
912A - Tricky Alchemy
1249A - Yet Another Dividing into Teams
1713C - Build Permutation
1699A - The Third Three Number Problem
1617B - GCD Problem